<?php

namespace SisesporteAdmin\Controller;

use Zend\View\Model\ViewModel;

class TurmaController extends CrudController
{

    public function __construct ()
    {
        $this->entity = "Sisesporte\Entity\Turma";
        $this->form = "SisesporteAdmin\Form\TurmaForm";
        $this->service = "Sisesporte\Service\TurmaService";
        $this->controller = "turma";
        $this->route = "sisesporte-admin";
    }

    protected $msg;

    public function newAction ()
    {
        $form = new $this->form(NULL, $this->getEm());
        $request = $this->getRequest();

        if ($request->isPost()) {
            $array = $request->getPost();
            $array['stAtivo'] = TRUE;
            $form->setData($array);
//            if ($form->isValid()) {
            $service = $this->getServiceLocator()->get($this->service);

            if ($service->insert($request->getPost()->toArray())) {
                $this->msg = TRUE;
            } else {
                $this->flashMessenger()->addMessage("Falha ao Salvar!")->addInfoMessage('error');
                return $this->redirect()->toRoute($this->route, array('controller' => $this->controller,
                            'action' => 'new'));
            }
//            }
        }

        return new ViewModel(array('form' => $form, 'msg' => $this->msg));
    }

    public function editAction ()
    {
        $form = new $this->form(NULL, $this->getEm());
        $request = $this->getRequest();

        $repository = $this->getEm()->getRepository($this->entity);
        $entity = $repository->find($this->params()->fromRoute('id', 0));

        if ($this->params()->fromRoute('id', 0)) {
            $array = $entity->toArray();
            $array['stAtivo'] = TRUE;
            $form->setData($array);
        }

        if ($request->isPost()) {
            $form->setData($request->getPost());
//            if ($form->isValid()) {
            $service = $this->getServiceLocator()->get($this->service);
            $service->update($this->params()->fromRoute('id', 0), $request->getPost()->toArray());
            $this->msg = TRUE;
//              }
        }

        return new ViewModel(array('form' => $form, 'entity' => $entity->toArray(), 'msg' => $this->msg));
    }

    /**
     * Retorna Array de Turmas Abertas de acordo com a Modalidade selecionada
     * @param type int $sqModalidade
     * @return array Turma
     */
    public function getTurmaAbertaByModalidadeAction ()
    {
        if ($this->getRequest()->isPost()) {
            $param = $this->getRequest()->getPost();
            $service = $this->getServiceLocator()->get("Sisesporte\Service\TurmaService");
            $data = $service->getTurmaAbertaByModalidade($param['sqModalidade']);
            $viewModel = new ViewModel(
                    array(
                'data' => $data,
                'flag' => $param['flag']
                    )
            );

            return $viewModel->setTerminal(TRUE);
        }
    }

    public function deleteAction ()
    {
        $service = $this->getServiceLocator()->get($this->service);
        $preMatricula = $service->findPreMatriculaVinculada($this->params()->fromRoute('id', 0));
        $servicePreMatricula = $this->getServiceLocator()->get("Sisesporte\Service\PreMatriculaService");
        
        foreach($preMatricula as $pmid) {
            $servicePreMatricula->delete($pmid->getSqPreMatricula());
        }
        
        $service->delete($this->params()->fromRoute('id', 0));
        
        
        $this->msg = TRUE;
        
        return new ViewModel(array('msg' => $this->msg));
    }

}
